package com.chj.acwing.zhou211;

import java.util.HashMap;
import java.util.Map;

public class Code01_LargestSubstringBetweenTwoEqualCharacters {
	public int maxLengthBetweenEqualCharacters(String s) {

		char[] sChar = s.toCharArray();
		int res = -1;
		for (char c = 'a'; c <= 'z'; c++) {
			int l = sChar.length, r = 0;
			for (int i = 0; i < sChar.length; i++) {
				if (sChar[i] == c) {
					l = Math.min(l, i);
					r = Math.max(r, i);
				}
			}

			res = Math.max(res, r - l - 1);
		}
		return res;
	}

	public int maxLengthBetweenEqualCharacters02(String s) {
		Map<Character, Integer> map = new HashMap<>();
		int max = -1;
		for (int i = 0; i < s.length(); i++) {
			if (!map.containsKey(s.charAt(i))) {
				map.put(s.charAt(i), i);
				continue;
			}
			Integer integer = map.get(s.charAt(i));
			max = Math.max(max, i - integer - 1);
		}
		return max;
	}
}
